﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.skill;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using System.Threading;

namespace l1cs.server.model
{
    /// <summary>
    /// L1ParalysisPoisonと被るコードが多い。特にタイマー。何とか共通化したいが難しい。
    /// </summary>
    public class L1CurseParalysis : L1Paralysis
    {
        private L1Character _target;
        private int _delay;
        private int _time;

        private Timer _timer;

        //private class ParalysisDelayTimer extends Thread {
        //    @Override
        public void ParalysisDelayTimerSub( object state )
        {
            _target.setSkillEffect( L1SkillId.STATUS_CURSE_PARALYZING, 0 );

            //Thread.Sleep( _delay ); // 麻痺するまでの猶予時間を待つ。
            //} catch (InterruptedException e) {
            //_target.killSkillEffectTimer( L1SkillId.STATUS_CURSE_PARALYZING );
            //    return;
            //}

            if ( _target is L1PcInstance )
            {
                L1PcInstance player = (L1PcInstance)_target;
                if ( !player.isDead() )
                {
                    player.sendPackets( new S_Paralysis( 1, true ) ); // 麻痺状態にする
                }
            }
            _target.setParalyzed( true );
            //_timer = new ParalysisTimer();
            //GeneralThreadPool.getInstance().execute(_timer); // 麻痺タイマー開始
            _timer = new Timer( new TimerCallback( ParalysisTimerSub ), null, _time, Timeout.Infinite );
            //if ( isInterrupted() )
            //{
            //    _timer.interrupt();
            //}
        }
        //}

        //private class ParalysisTimer extends Thread {
        //    @Override
        public void ParalysisTimerSub( object state )
        {
            _target.killSkillEffectTimer( L1SkillId.STATUS_CURSE_PARALYZING );
            _target.setSkillEffect( L1SkillId.STATUS_CURSE_PARALYZED, 0 );

            //Thread.Sleep( _time );

            _target.killSkillEffectTimer( L1SkillId.STATUS_CURSE_PARALYZED );
            if ( _target is L1PcInstance )
            {
                L1PcInstance player = (L1PcInstance)_target;
                if ( !player.isDead() )
                {
                    player.sendPackets( new S_Paralysis( 1, false ) ); // 麻痺状態を解除する
                }
            }
            _target.setParalyzed( false );
            cure(); // 解呪処理
        }
        //}

        private L1CurseParalysis( L1Character cha, int delay, int time )
        {
            _target = cha;
            _delay = delay;
            _time = time;

            curse();
        }

        private void curse()
        {
            if ( _target is L1PcInstance )
            {
                L1PcInstance player = (L1PcInstance)_target;
                player.sendPackets( new S_ServerMessage( 212 ) );
            }

            _target.setPoisonEffect( 2 );

            // TODO 優先度9 タイマースレッド
            //_timer = new ParalysisDelayTimer();
            //GeneralThreadPool.getInstance().execute(_timer);
            _timer = new Timer( new TimerCallback( ParalysisDelayTimerSub ), null, _delay, Timeout.Infinite );
        }

        public static bool curse( L1Character cha, int delay, int time )
        {
            if ( !( cha is L1PcInstance || cha is L1MonsterInstance ) )
            {
                return false;
            }
            if ( cha.hasSkillEffect( L1SkillId.STATUS_CURSE_PARALYZING )
                    || cha.hasSkillEffect( L1SkillId.STATUS_CURSE_PARALYZED ) )
            {
                return false; // 既に麻痺している
            }

            cha.setParalaysis( new L1CurseParalysis( cha, delay, time ) );
            return true;
        }

        override
        public int getEffectId()
        {
            return 2;
        }

        override
        public void cure()
        {
            if ( _timer != null )
            {
                _timer.Dispose(); // 麻痺タイマー解除
            }

            _target.setPoisonEffect( 0 );
            _target.setParalaysis( null );
        }
    }
}
